package org.vergil.core.service;

import org.springframework.stereotype.Service;
import org.vergil.core.dto.User;

import java.util.Optional;

/**
 * Created by vergil on 17-3-12.
 */
@Service
public class TestOptionalService {

    public User getUserNull(){
        return null;
    }

    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("nero");
        user.setPassword("nero");
        user.setStatus(1);
        return user;
    }

    /**
     * 测试Optional的使用
     * @return
     */
    public User Test(){
        Optional<User> user = Optional.ofNullable(getUserNull());
        User user1 = new User();
        user1.setId(2);
        user1.setName("nero2");
        user1.setPassword("nero2");
        user1.setStatus(2);
//        return user.orElseGet(() -> getUser());//如果user为空调用getUser()方法
        return user.orElse(user1);//如果user为空则返回user1
    }

}
